var InterfaceUser = function () { 

    var basePath = ROOT_PATH + 'interfaceUser/',
        pagePath = ROOT_PATH + 'pages/interfaceUser/',
        $dataGrid = null,
        noSelectedMsg = '请选择';


    /**
     * 初始化数据表格
     * @param gridId
     */
    function initGrid(gridId) {
        $dataGrid = BsGridUtil.initDataGrid({
            gridId: gridId,
            url: basePath + 'paging',
            singleSelect: false,
            columns: [[
                {field: 'id', title: 'id', hidden: false, checkbox: true},
                {field: 'userName', title:'账户名称', hidden: false, sortable: true, width:100},
                {field: 'remarks', title:'备注', hidden: false, sortable: true, width:100},
                {
                	field: 'isEnable', title: '是否启用（0启用  1停用）', hidden: false, sortable: true, width: 100,
                    formatter: function (val, row) {
                        return row.isEnable == null ? "" : row.isEnable.name;
                    }
                },
                {
                    field: 'action', title: '操作',
                    formatter: function (value, row, index) {
                        var html = '<a href="javascript:InterfaceUser.edit(' + row.id + ')"><div class="btn-sml icon-edit" title="修改"></div></a>';
                        html += '<a href="javascript:InterfaceUser.info(' + row.id + ')"><div class="btn-sml icon-info" title="详情"></div></a>';
                        html += '<a href="javascript:InterfaceUser.del(' + row.id + ')"><div class="btn-sml icon-remove" title="删除"></div></a>';
                        return html;
                    }
                }
            ]]
        });
    }

    /**
     * 删除
     */
    function del(id) {
        if (id) {
            Dialog.confirm('确认删除', function (r) {
                $.Ajax({
                    url: basePath + "delete",
                    data: {id: id},
                    success: function () {
                        Dialog.success("删除成功！");
                        reloadDataGrid();
                    }
                });
            });
        } else {
            Dialog.warning(noSelectedMsg);
        }
    }


    /**
     * 获取当前已选择的interface_user
     * @returns {*|{js, css, dependencies}}
     */
    function getSelectedInterfaceUser() {
        return $dataGrid.datagrid('getSelected');
    }

    /**
     * 编辑
     */
    function edit(id) {
        if (id) {
            $.Get(basePath + 'get', {id: id}, function (data) {
                var dlg = FormUtils.openDialog({
                    domId: 'editDl', //打开dialog的容器
                    href: pagePath + 'interfaceUser-edit.html',
                    url: basePath + 'edit',
                    success: function (data) {
                        reloadDataGrid();
                    }, //成功时候执行的方法
                    onLoad: function () {
                        FormUtils.setValues($(dlg).find('form:first'), data.data);
                    },
                    title: '修改'
                });
            });
        } else {
            Dialog.warning(noSelectedMsg);
        }
    }

    /**
     * 刷新表格
     */
    function reloadDataGrid(){
        $dataGrid.datagrid('reload');
    }


    /**
     * 创建
     */
    function create() {
        FormUtils.openDialog({
            domId: 'createDl', //打开dialog的容器
            href: pagePath + 'interfaceUser-create.html',
            url: basePath + 'create',
            success: function (data) {
                reloadDataGrid();
            }, //成功时候执行的方法
            title: '创建'
        });
    }

    /**
     * 详情
     * @param id
     */
    function info(id){
        if(id){
            $.Get(basePath + 'get', {id: id}, function (data) {
                var dlg = FormUtils.openDialog({
                    domId: 'infoDl', //打开dialog的容器
                    href: pagePath + 'interfaceUser-info.html',
                    buttons: [{
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            dlg.panel('close');
                        }
                    }],
                    onLoad: function () {
                        FormUtils.setValues($(dlg).find('form:first'), data.data);
                    },
                    title: '详情'
                });
            });
        }else {
            Dialog.warning(noSelectedMsg);
        }
    }

    /**
     * 批量删除
     */
    function batchDelete() {
        var selections = $dataGrid.datagrid('getSelections');
        if(selections && selections.length > 0){
            Dialog.confirm('确认删除interface_user', function (r) {
                var roleIds = [];
                $.each(selections,function(i,role){
                    roleIds.push(role.id);
                });

                $.Ajax({
                    url: basePath + "batchDelete",
                    data: {ids: roleIds},
                    success: function () {
                    Dialog.success("删除成功！");
                        reloadDataGrid();
                    }
                 });
            });
        } else {
          Dialog.warning(noSelectedMsg);
        }

    }

    return {
        initGrid: initGrid,
        create: create,
        edit: edit,
        del: del,
        batchDelete:batchDelete,
        info:info
    }
}();